import * as cc from "cc";
import yyComponent from "../../Script/Common/yyComponent";
import { GlobalEnum } from "../../Script/GameSpecial/GlobalEnum";
import { EventType } from "../../Script/GameSpecial/GameEventType";
import GlobalData from "../../Script/Common/GlobalData";
import PlayerData from "../../Script/Common/PlayerData";
import Action3dManager, { ActionMngType } from "../../Script/Common/Action3dManager";

const { ccclass, property } = cc._decorator;

@ccclass
export default class WinUI extends yyComponent {

    /**场景/UI类型 */
    public get uiType() { return GlobalEnum.UI.winUI; }

    public init(data?) {
        this.initGold();
        if (undefined !== data) {
            this.setData(data);
        }
    }
    protected setData(data) {
        this.goldData.levelGold = data.gold;
        this.goldData.scoreRate = data.scoreRate;
        this.goldData.winGold = 20;
        this.goldData.curGold = 0;
        this.goldData.totalGold = this.getTotalGold();
        this.scoreRate.string = "/" + this.goldData.scoreRate.toFixed(1);
        this.totalGold.string = "0";

        this.scoreRate.node.setScale(0, 0, 0);
        setTimeout(() => {
            let node = this.scoreRate.node;
            let s = 6;
            node.setScale(s, s, s);
            let d = 0.1;
            s = 1;
            let scale0 = Action3dManager.scaleTo(d, s, s, s);
            let cb = Action3dManager.callFun(() => {
                this.schedule(this.threadGoldRate, 0.03, 50, 0.5);
            });
            let seq = Action3dManager.sequence(scale0, cb);
            Action3dManager.getMng(ActionMngType.UI).runAction(node, seq);
        }, 1000);

    }
    public reset() {
        this.resetGold();
    }

    public show(data?) {
        this.reset();
        this.node.active = true;
        if (undefined !== data) {
            this.setData(data);
        }
        this.emit(EventType.Common.UIEvent.enter, GlobalEnum.UI.playerAssetBar);
    }

    //#region 奖励内容
    @property(cc.Label)
    protected scoreRate: cc.Label = null;
    @property(cc.Label)
    protected totalGold: cc.Label = null;
    protected goldData: { levelGold: number, scoreRate: number, winGold: number, curGold: number, totalGold: number } = null;
    protected awarded: boolean = false;
    protected initGold() {
        this.awarded = false;
        this.goldData = {
            levelGold: 0,
            scoreRate: 1,
            winGold: 0,
            curGold: 0,
            totalGold: 0,
        };
    }
    protected resetGold() {
        this.awarded = false;
    }
    protected getTotalGold() {
        return Math.floor(this.goldData.levelGold * this.goldData.scoreRate) + this.goldData.winGold;
    }
    protected threadGoldRate() {
        this.goldData.curGold += Math.ceil(this.goldData.totalGold * 0.03);
        if (this.goldData.curGold >= this.goldData.totalGold) {
            this.goldData.curGold = this.goldData.totalGold;
            this.unschedule(this.threadGoldRate);
        }
        this.totalGold.string = this.goldData.curGold.toString();
    }
    //#endregion

    /*******************************************************按钮回调*******************************************************/
    protected onBtnGetAward() {
        this.playClickBtn();
        this.addGold(this.getTotalGold(), this.enterLobby.bind(this));
    }
    //视频多倍领取
    protected onBtnVideo() {
        this.playClickBtn();
        this.emit(EventType.Common.SDKEvent.showVideo, {
            success: () => {
                this.addGold(this.getTotalGold() * 3, this.enterLobby.bind(this));
            },
        });
    }

    /**播放金币动画获得金币 */
    protected addGold(gold: number, cb?: Function) {
        this.emit(EventType.Common.UIEvent.playGoldAnim, {
            cb: () => {
                this.emit(EventType.Common.PlayerDataEvent.updatePlayerData, {
                    attribute: "gameData.asset.gold",
                    value: gold,
                    mode: "+",
                    emit: true,
                    save: true,
                });
                this.awarded = true;
                !!cb && cb();
            },
            gold: gold,
        });
    }

    protected enterLobby() {
        this.emit(EventType.Common.DirectorEvent.enterLobby);
    }

    protected onBtnMoreGame() {
        this.emit(EventType.Common.SDKEvent.showAppBox);
    }
    protected onBtnShare() {
        this.emit(EventType.Common.SDKEvent.shareToFriends);
    }

}
